{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# chart module\n",
    "\n",
    "This module relies on `pygal` library, so the returned charts are instances of `pygal.chart`. See options at \n",
    "[pygal site][1]\n",
    "\n",
    "I made a JavaScript 'equivalent': https://code.earthengine.google.com/b2922b860b85c1120250794fb82dfda8\n",
    "\n",
    "  [1]: http://www.pygal.org/en/latest/documentation/index.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from geetools import ui"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_site = ee.Geometry.Point([-71, -42])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_feat = ee.Feature(test_site, {'name': 'test feature'})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_featcol = ee.FeatureCollection([\n",
    "    test_feat, \n",
    "    test_feat.buffer(100).set('name', 'buffer 100'),\n",
    "    test_feat.buffer(1000).set('name', 'buffer 1000')\n",
    "])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Time Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "years = ee.List([2015, 2016, 2017, 2018])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "col = ee.ImageCollection('COPERNICUS/S2').filterBounds(test_site)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def make_time_series(year):\n",
    "    ''' make a time series from year's list '''\n",
    "    eefilter = ee.Filter.calendarRange(year, field='year')\n",
    "    filtered = col.filter(eefilter)\n",
    "    return filtered.mean().set('system:time_start', ee.Date.fromYMD(year, 1, 1).millis())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "time_series = ee.ImageCollection(years.map(make_time_series))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Chart *series*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "chart_ts = ui.chart.Image.series(**{\n",
    "    'imageCollection': time_series, \n",
    "    'region': test_site,\n",
    "    'scale': 10,\n",
    "    'bands': ['B1', 'B2', 'B3'],\n",
    "    # 'xProperty': 'B4', # You can use a band too!\n",
    "    'labels': ['band B1', 'B2 band', 'this is B3']\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7a81b34e7c324831a3b8db3320387803",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HTML(value=u'<embed src=data:image/svg+xml;charset=utf-8;base64,PD94bWwgdmVyc2lvbj0nMS4wJyBlbmNvZGluZz0ndXRmLT…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "chart_ts.render_widget(width='50%')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Chart *seriesByRegion*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "chart_ts_region = ui.chart.Image.seriesByRegion(**{\n",
    "    'imageCollection': time_series,\n",
    "    'reducer': ee.Reducer.median(),\n",
    "    'regions': test_featcol,\n",
    "    'scale': 10,\n",
    "    'band': 'B11',\n",
    "    'seriesProperty': 'name'\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0ec521d0cac749c38f8409abea42c75f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HTML(value=u'<embed src=data:image/svg+xml;charset=utf-8;base64,PD94bWwgdmVyc2lvbj0nMS4wJyBlbmNvZGluZz0ndXRmLT…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "chart_ts_region.render_widget(height=500)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
